<?php
/**
 * Pertyler DatabaseManagement
 * Control
 * Table
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pertyler;

require_once __DIR__ . '/main.php';

if(isset($_POST['Select']))
	return require __DIR__ . '/SearchedRowArray.php';

if(isset($_POST['SelectUnpublished']))
	return require __DIR__ . '/SearchedUnpublishedRowArray.php';

if(!isset($_GET['Table']))
	throw new \Exception('Table parameter required.');

if(!isset($_GET['Row']))
	throw new \Exception('Row parameter required.');

require_once __DIR__ . '/../databaseModel/Role.php';

if(!($moderator = Role_Moderator::Select()))
	return require __DIR__ . '/Index.php';

require_once __DIR__ . '/../databaseModel/Table.php';

if(!($table = Table::Select($_GET['Table'])))
	throw new \Exception('Table cannot found.');

if(!$table->SelectPrivilege('select'))
	throw new \Exception('Table does not have select privilege.');

if(!Role::Select()->SelectRolePrivilege($table->SelectPrivilege('select')))
	throw new \Exception('Select privilege required.');

if(!($row = $table->SelectRow($_GET['Row'])))
	throw new \Exception('Row cannot found.');

require_once __DIR__ . '/../library/Pert/General/Page.php';

\Pert\Page::Get()->AddTitle($table);

\Pert\Page::Get()->AddTitle($row);

$Content = \Pert\Page::Get()->Content();

$Content->TableName = (string) $table;

$Content->RowName = (string) $row;

unset($table);

unset($row);

unset($_GET);

unset($_POST);

echo \Pert\Page::Get()->HeaderHTML();

require __DIR__ . '/../view/Row.php';

echo \Pert\Page::Get()->FooterHTML();

return true;
